package com.fqy.gmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fqy.gmall.model.product.BaseCategoryTrademark;
import com.fqy.gmall.model.product.BaseTrademark;
import com.fqy.gmall.model.product.CategoryTrademarkVo;
import com.fqy.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.fqy.gmall.product.mapper.BaseTrademarkMapper;
import com.fqy.gmall.product.service.BaseCategoryTrademarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * User: fqy
 * Date: 2021/9/11
 * Time: 14:41
 * Describe:
 */
@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;
    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    //根据category3Id获取品牌列表
    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        //根据category3Id获取trademark_id
        //select * from base_category_trademark where  = 61;
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id", category3Id);
        List<BaseCategoryTrademark> categoryTrademarkList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkQueryWrapper);
        //根据trademark_id 查询 base_trademark 获取 分类品牌列表
        //select * from base_trademark where id in (1,3);
        /**
         * 法一：for循环
         * List<Long> idList = new ArrayList<>();
         *         //  获取到品牌Id 集合！
         *         for (BaseCategoryTrademark baseCategoryTrademark : categoryTrademarkList) {
         *             idList.add(baseCategoryTrademark.getTrademarkId());
         *         }
         */
        /**
         * 法二：lambda表达式
         */
        if (!CollectionUtils.isEmpty(categoryTrademarkList)) {
            //  只有函数式接口才能使用拉姆达表达式！
            List<Long> idList = categoryTrademarkList.stream().map((baseCategoryTrademark) -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());

            return baseTrademarkMapper.selectBatchIds(idList);
        }
        return null;

    }

    //  根据分类Id 获取可选的品牌数据
    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        //根据category3Id获取trademark_id
        //select * from base_category_trademark where  = 61;
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id", category3Id);
        List<BaseCategoryTrademark> categoryTrademarkList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkQueryWrapper);
        //根据trademark_id 查询 base_trademark 获取 分类品牌列表
        //select * from base_trademark where id in (1,3);
        //  判断：
        if (!CollectionUtils.isEmpty(categoryTrademarkList)) {
            //  1,3
            List<Long> idList = categoryTrademarkList.stream().map((baseCategoryTrademark) -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            //  只留2！
            //  baseTrademarkMapper.selectList(null) 1,2,3
            return baseTrademarkMapper.selectList(null).stream().filter((baseTrademark) -> {
                return !idList.contains(baseTrademark.getId());
            }).collect(Collectors.toList());
        }
        //  .map(); 做映射的！ filter(); 对当前集合数据进行过滤！
        //  当这个分类Id 下没有品牌的时候， 显示所有品牌数据！
        return baseTrademarkMapper.selectList(null);
    }

    // 移除品牌列表数据
    @Override
    public void remove(Long category3Id, Long trademarkId) {
        //  delete from base_category_trademark where trademark_id = trademarkId and category3_id=category3Id;
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id", category3Id);
        baseCategoryTrademarkQueryWrapper.eq("trademark_id", trademarkId);
        //  删除数据
        baseCategoryTrademarkMapper.delete(baseCategoryTrademarkQueryWrapper);
    }

    // 保存分类品牌列表数据
    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        //  本质插入数据 base_category_trademark
        //  获取到对应的tmId 1,3
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        //  循环遍历
        if (!CollectionUtils.isEmpty(trademarkIdList)) {
//            List<BaseCategoryTrademark> baseCategoryTrademarkList = new ArrayList<>();
            //  循环遍历！
//            for (Long tmId : trademarkIdList) {
//                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
//                baseCategoryTrademark.setTrademarkId(tmId);
//                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
//                baseCategoryTrademarkList.add(baseCategoryTrademark);
//                //  baseCategoryTrademarkMapper.insert(baseCategoryTrademark);
//            }
//              拉姆达表达式真好！
            List<BaseCategoryTrademark> collect = trademarkIdList.stream().map((tmId) -> {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setTrademarkId(tmId);
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                return baseCategoryTrademark;
            }).collect(Collectors.toList());

            //  保存！
            this.saveBatch(collect);

            //  保存！
//            this.saveBatch(baseCategoryTrademarkList);
        }
        //  有可能一条，也有可能多条!
    }

}
